﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasketStats.Core.Stats;
using BasketStats.Core.Team;
using BasketStats.Core.Game;
using System.Windows.Forms.DataVisualization.Charting;

namespace BasketStats
{
    public partial class PlayingPlayerView : UserControl
    {
        private Player m_player;

        public Player Player
        {
            get
            {
                return m_player;
            }
            set
            {
                if (value != m_player)
                {
                    if (m_player != null)
                    {
                        m_player.CurrentStatistics.Shots.CollectionChanged -= (o, e) => Refresh();
                        m_player.CurrentStatistics.PropertyChanged -= (o, e) => Refresh();
                    }

                    m_player = value;
                    m_player.CurrentStatistics.Shots.CollectionChanged += (o, e) => Refresh();
                    m_player.CurrentStatistics.PropertyChanged += (o, e) => Refresh();
                    Refresh();
                }
            }
        }

        public PlayingPlayerView()
        {
            InitializeComponent();
        }

        private void btnOffensiveRebound_Click(object sender, EventArgs e)
        {
            if (m_player != null)
            {
                m_player.CurrentStatistics.OffensiveRebounds += 1;
            }
        }

        private void btnShot_Click(object sender, EventArgs e)
        {
            if (m_player != null)
            {
                var court = new Court() { Player = m_player };
                court.ShowDialog();
            }
        }

        public override void Refresh()
        {
            if (m_player != null)
            {
                txtPoints.Text = m_player.CurrentStatistics.Shots.Where(x => x.Scored).Sum(x => (int)x.Type).ToString();
                lblNumber.Text = m_player.Info.Number.ToString();
                base.Refresh();
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (m_player != null)
            {
                foreach (var shot in m_player.CurrentStatistics.Shots.Where(s => s.Type != ShotType.FT)) // Do not show Free Throws
                {
                    var color = (shot.Scored) ? Color.Green : Color.Red;
                    e.Graphics.DrawEllipse(new Pen(new SolidBrush(color)), (int)Math.Round(shot.x * picShotRecap.Size.Width), (int)Math.Round(shot.y * picShotRecap.Size.Height), 6, 6);
                }
            }
        }

        private void Shots_Click(object sender, EventArgs e)
        {
            var frm = new ChartTestForm { Player = m_player };
            frm.ShowDialog();
        }
    }
}
